package com.dml.webmagicd.webmagic.pipeline;

import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.io.FileWriter;
import java.nio.charset.StandardCharsets;
import java.util.Map;

@Component
public class JuejinPipeline implements Pipeline {
  @Override
  public void process(ResultItems resultItems, Task task) {
    Map<String, Object> all = resultItems.getAll();
    String title = (String) all.get("title");
    int sectionIndex = (int) all.get("sectionIndex");
    String content = (String) all.get("content");
    String filename = "/Users/dumenglong/Desktop/nestjs/" + sectionIndex +
            "." + title + ".md";
    try(FileWriter fileWriter = new FileWriter(filename, StandardCharsets.UTF_8)) {
      fileWriter.write(content);
    } catch (Exception e) {
      System.out.println(e);
    }
  }
}
